package Screens;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import resource.HighScoreStore;
import resource.Keyboard;
import resource.SaveScoreScreen;

import Game.CanvasGame;
import Objects.Statistic;

public class SaveScore extends Screen {
	private Image[] img;
	private static Statistic p;
	private static String name;
	public SaveScore(CanvasGame _canvas, Statistic p) {
		super(_canvas);
		SaveScore.p = p;
		name = "";
		img = new Image[SaveScoreScreen.IMG_LENGHT];
		load_screen();
	}

	public void load_screen() {
		// TODO Auto-generated method stub
		for (int i = 0; i < SaveScoreScreen.IMG_LENGHT; i++) {
			load(i);
		}
	}
	
	public void un_load_screen() {
		// TODO Auto-generated method stub
		//img = null;
		if(img == null)    return;
        for (int i = 0; i < SaveScoreScreen.IMG_LENGHT; i++) {
            unLoad(i);
        }
	}


	public boolean load(int i) {
		/* TODO: load the images here */
	    try {
	    	img[i] = Image.createImage(SaveScoreScreen.imgSource[i]);
	    } catch (Exception ex) {
	    	System.err.println(ex.toString()+"aaaa");
	    	return false;
	    }
	    return true;
	}
	
	public void unLoad(int i) {
        /* TODO: make sure the object get's destroyed */
        img[i] = null;
    }

	public void keyEvent(int keycode, int event) {
		// TODO Auto-generated method stub
		if(event == Keyboard.KEY_EVENT_DOWN){
            switch(keycode){
                case Keyboard.KEY_OK:
                	ActionClick(keycode);
                    break;
                case Keyboard.KEY_CLEAR:
                case Keyboard.KEY_RIGHT_SOFTKEY:
                	name = name.length()==0?name:name.substring(0, name.length()-1);
                	break;
                default:
                	name = name + (char)keycode;
                	break;
            }
        }
	}

	public void ActionClick(int i) {
		// TODO Auto-generated method stub
		if(i == Keyboard.KEY_OK){
			p.setName(name);
			HighScoreStore a = new HighScoreStore();
			a.open();
			a.replaceWith(p);
			a.close();
			a = null;
			un_load_screen();
			canvas.setScreen(new HighScore(canvas));
		}
	}

	public void update() {
		// TODO Auto-generated method stub

	}

	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		
		g.drawImage(img[SaveScoreScreen.BG], 0, 0, Graphics.TOP|Graphics.LEFT);
		g.drawImage(img[SaveScoreScreen.MENU], 0, 240, Graphics.BOTTOM|Graphics.LEFT);
		g.drawImage(img[SaveScoreScreen.FORM], 20, 50, Graphics.TOP|Graphics.LEFT);
		g.drawString(name+"_", 120, 90, Graphics.TOP|Graphics.LEFT);
		g.drawString(String.valueOf(p.getWinCount()), 120, 155, Graphics.BOTTOM|Graphics.LEFT);
		g.drawString(String.valueOf(p.getLoseCount()), 120, 185, Graphics.BOTTOM|Graphics.LEFT);
		g.setColor(0, 0, 0);
		Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
		g.setFont(font);
		g.drawString("Lưu", 160, 238, Graphics.HCENTER|Graphics.BOTTOM);
		g.drawString("Xóa", 320, 238, Graphics.RIGHT|Graphics.BOTTOM);
	}

}
